#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "./start_mplayer/start_mplayer.h"
#include "./lrc_lib/lrc_lib.h"
#include "./pos/console.h"

int main(int argc, char const *argv[])
{
    char *text = get_text("./music.text/兰亭序-周杰伦.lrc");

    int seg_len = 0;
    char *seg_text[200] = {NULL};
    segmented_text(seg_text,&seg_len,text);

    char *title[10] = {NULL};
    int title_len = 0;
    get_title(seg_text,title,&title_len);

    LRC* head = NULL;
    get_link(seg_text,seg_len,title_len,&head);

    printf_title(title,title_len);

    TIME time = {0,0};
    printf_time(time,title_len+1);

    printf_first_fews(head,title_len+2);
    int x = 100000;
    float node_time = 0.0;
    while (1)
    {
        if (node_time == 0.0)
        {
           mplayer_play("./music.text/兰亭序-周杰伦.mp3");
        }

        printf_time(time,title_len+1);
        LRC *current_node = find_current_node(head,node_time);
        if (current_node != NULL)
        {
            if (current_node->next == NULL)
            {
                break;
            }    
            printf_title(title,title_len);
            printf_time(time,title_len+1);
            LRC* printf_node = get_printf_node(current_node,head);
            printf_lrc(current_node,printf_node,title_len+2);  
                  
        }
        usleep(x--);
        node_time += 0.1;
        set_time(&time);
    }
    clear_screen();
    cusor_moveto(1, 1);
    doubly_link_free(head);
    title_free(title,title_len);
    return 0;
}
